import { create } from 'zustand';

// 定义审计日志类型
export interface AuditLog {
  id: string;
  timestamp: string;
  userId: string;
  username: string;
  eventType: string;
  resourceType: string;
  resourceId: string;
  action: string;
  status: string;
  ipAddress: string;
  details: string;
}

// 定义审计日志存储状态
interface AuditLogState {
  logs: AuditLog[];
  loading: boolean;
  filters: {
    dateRange: [string, string] | null;
    userIds: string[];
    eventTypes: string[];
    resourceTypes: string[];
    status: string[];
    searchText: string;
  };
  fetchLogs: () => Promise<void>;
  setFilters: (filters: Partial<AuditLogState['filters']>) => void;
  clearFilters: () => void;
}

// 创建审计日志存储
export const useAuditLogStore = create<AuditLogState>((set, get) => ({
  logs: [],
  loading: false,
  filters: {
    dateRange: null,
    userIds: [],
    eventTypes: [],
    resourceTypes: [],
    status: [],
    searchText: '',
  },
  
  fetchLogs: async () => {
    set({ loading: true });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 模拟数据
      const mockLogs: AuditLog[] = Array.from({ length: 50 }).map((_, index) => ({
        id: `log-${index}`,
        timestamp: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),
        userId: `user-${Math.floor(Math.random() * 10)}`,
        username: `用户${Math.floor(Math.random() * 10)}`,
        eventType: ['登录', '文档创建', '文档更新', '文档删除', '系统配置'][Math.floor(Math.random() * 5)],
        resourceType: ['用户', '文档', '模型', '系统'][Math.floor(Math.random() * 4)],
        resourceId: `resource-${Math.floor(Math.random() * 100)}`,
        action: ['创建', '读取', '更新', '删除', '下载'][Math.floor(Math.random() * 5)],
        status: ['成功', '失败', '警告'][Math.floor(Math.random() * 3)],
        ipAddress: `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`,
        details: `操作详情-${Math.random().toString(36).substring(2, 10)}`,
      }));
      
      set({ logs: mockLogs, loading: false });
    } catch (error) {
      console.error('获取审计日志失败:', error);
      set({ loading: false });
    }
  },
  
  setFilters: (filters) => {
    set((state) => ({
      filters: { ...state.filters, ...filters }
    }));
  },
  
  clearFilters: () => {
    set({
      filters: {
        dateRange: null,
        userIds: [],
        eventTypes: [],
        resourceTypes: [],
        status: [],
        searchText: '',
      }
    });
  },
})); 